package cn.iocoder.an.module.business.service.dataCollection;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.an.module.business.controller.admin.dataCollection.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.dataCollection.DevInfoDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.business.dal.mysql.dataCollection.DevInfoMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.business.enums.ErrorCodeConstants.*;

/**
 * 设备台账 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class DevInfoServiceImpl implements DevInfoService {

    @Resource
    private DevInfoMapper devInfoMapper;

    @Override
    public String createDevInfo(DevInfoSaveReqVO createReqVO) {
        // 插入
        DevInfoDO devInfo = BeanUtils.toBean(createReqVO, DevInfoDO.class);
        devInfoMapper.insert(devInfo);

        // 返回
        return devInfo.getDevid();
    }

    @Override
    public void updateDevInfo(DevInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateDevInfoExists(updateReqVO.getDevid());
        // 更新
        DevInfoDO updateObj = BeanUtils.toBean(updateReqVO, DevInfoDO.class);
        devInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteDevInfo(String id) {
        // 校验存在
        validateDevInfoExists(id);
        // 删除
        devInfoMapper.deleteById(id);
    }

    @Override
        public void deleteDevInfoListByIds(List<String> ids) {
        // 删除
        devInfoMapper.deleteByIds(ids);
        }


    private void validateDevInfoExists(String id) {
        if (devInfoMapper.selectById(id) == null) {
            throw exception(DEV_INFO_NOT_EXISTS);
        }
    }

    @Override
    public DevInfoDO getDevInfo(String id) {
        return devInfoMapper.selectById(id);
    }

    @Override
    public PageResult<DevInfoDO> getDevInfoPage(DevInfoPageReqVO pageReqVO) {
        return devInfoMapper.selectPage(pageReqVO);
    }

}